package com.card.hwatt.controllers;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.card.hwatt.assembler.*;
import com.card.hwatt.service.*;
import com.card.hwatt.core.RequestJsonParam;
import com.card.hwatt.dto.*;

@Controller
@Scope("prototype")
@RequestMapping("/overtime")
public class OvertimeController extends BaseController{

	@Autowired
	@Qualifier("overtimeService")
	private OvertimeService overtimeService;

	@RequestMapping("/index")
	public String index() {
		return "overtime";
	}

	@RequestMapping("/loadAll")
	public @ResponseBody
	PageResult<Overtime> loadAll(Integer id, String name, String startDate,
			String endDate, int page, int rows) {

		if (id == null) {
			PageResult<Overtime> result = new PageResult<Overtime>();
			result.rows = new ArrayList<Overtime>();
			return result;
		}
		
		StringBuilder where = new StringBuilder(" employee.department.path like ? ");
		List<Object> param = new ArrayList<Object>();
		param.add("%@" + id + "@%");

		if (StringUtils.isNotEmpty(name)) {
			param.add("%" + name + "%");
			param.add("%" + name + "%");

			where.append(" and (employee.name like ? or employee.employeeCode like ? ) ");
		}

		if (StringUtils.isNotEmpty(startDate)
				&& StringUtils.isNotEmpty(endDate)) {

			String[] parsePatterns = { "yyyy-MM-dd", "yyyy-M-dd", "yyyy-M-d" };

			try {
				Date start = DateUtils.parseDate(startDate, parsePatterns);
				Date end = DateUtils.parseDate(endDate, parsePatterns);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(end);
				calendar.add(Calendar.DATE, 1);
				end = calendar.getTime();

				param.add(new java.sql.Date(start.getTime()));
				param.add(new java.sql.Date(end.getTime()));

				where.append(" and date >= ? and date < ? ");
			} catch (Exception e) {

			}
		}

		PageResult<Overtime> result = OvertimeAssembler.convert(overtimeService
				.loadAll(new PageParam(rows, page, where.toString(), param,
						" date desc ")));
		return result;
	}

	@RequestMapping("/save")
	public @ResponseBody
	ReturnValue save(@RequestJsonParam Overtime overtime) {
		if (overtime.id == null) {
			overtime.id = UUID.randomUUID();
		}
		overtime.date = new Date();
		overtimeService.save(OvertimeAssembler.convert(overtime));

		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}

	@RequestMapping("/delete")
	public @ResponseBody
	ReturnValue delete(@RequestParam UUID id) {
		overtimeService.delete(id);
		ReturnValue result = new ReturnValue();
		result.message = "ok";
		result.isSucess = true;
		return result;
	}
}
